from common.base import webdriver, By, sleep
from selenium.webdriver.common.action_chains import ActionChains
import os


class Mouse:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        self.driver.maximize_window()
        # 创建ac对象
        self.ac = ActionChains(driver=self.driver)

    def click(self):
        # 第一种方法
        self.driver.find_element(By.XPATH, '/html/body/form/input[3]').click()
        # 第二种方法
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[3]')
        # 需要使用perform来点击
        sleep(3)
        self.ac.click(btn).perform()

    def context_click(self):
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        # 右击
        sleep(3)
        self.ac.context_click(btn).perform()

    def double_click(self):
        btn = self.driver.find_element(By.XPATH, '/html/body/form/input[2]')
        # 双击
        sleep(3)
        self.ac.double_click(btn).perform()

    def drag_and_drop(self):
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')
        # 初始坐标
        btn1 = self.driver.find_element(By.XPATH, '//*[@id="dragger"]')
        # 拖动后放置的坐标
        sleep(3)
        btn2 = self.driver.find_element(By.XPATH, '/html/body/div[5]')
        self.ac.drag_and_drop(btn1, btn2).perform()

    def move_to_element(self):
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        btn = self.driver.find_element(By.XPATH, '/html/body/div[1]/div')
        # 悬停
        sleep(3)
        self.ac.move_to_element(btn).perform()

    def click_and_hold(self):
        # os.path.abspath('html') 获取html的绝对路径
        url = 'file:///' + os.path.abspath('html') + '/mouse_hold.html'
        self.driver.get(url)
        btn = self.driver.find_element(By.ID, 'btn1')
        sleep(3)
        self.ac.click_and_hold(btn).perform()

    def quit(self):
        sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    m1 = Mouse()
    m1.click()
    m1.double_click()
    m1.context_click()
    m1.drag_and_drop()
    m1.move_to_element()
    m1.click_and_hold()
    m1.quit()
